﻿using HotelManager.Common;
using HotelManager.Extensions;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace HotelManager.ViewModels
{
    public class MainViewModel : BindableBase, IConfigureService
    {
        private readonly IRegionManager regionManager;
        private readonly IContainerProvider container;
        private IRegionNavigationJournal journal;
        public MainViewModel(IRegionManager regionManager, IContainerProvider container) 
        {
            UserName = AppSession.UserName;
            this.regionManager = regionManager;
            this.container = container;
            NavigateCommand = new DelegateCommand<object>(Navigate);
            LogOutCommand = new DelegateCommand(() =>
            {
                App.LogOut(container);
            });
        }
        public DelegateCommand LogOutCommand { get; private set; }
        public DelegateCommand<object> NavigateCommand { get; private set; }
        private void Navigate(object obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.ToString()))
                return;
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.ToString(), back =>
            {
                journal = back.Context.NavigationService.Journal;
            });
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        public void Configure()
        {
            UserName = AppSession.UserName;
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView");
        }
    }
}
